/**
 * 病种列表初始化
 */
var SdsQcentity = {
    id: "entityTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    entityId: 0,
    entityDesc:null
};

SdsQcentity.onClickSource = function (e, treeId, treeNode) {
    if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体病种。");
        return false;
    }
    SdsQcentity.entityId = treeNode.id;
    SdsQcentity.entityDesc = treeNode.name;
    //重置中间区域
    SdsPrimaryHeader.seItem = null;
    SdsPrimaryHeader.primaryHeaderId = 0;
    $(".hisui-checkbox").checkbox("uncheck")
    //查询一级表头
    SdsPrimaryHeader.search();
    //查询二级表头
    SdsSecondaryHeader.search();
};


/**
 * 一级表头初始化
 */
var SdsPrimaryHeader = {
    id: "SdsPrimaryHeaderTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    primaryHeaderId: null,
    primaryHeaderDesc:null
};

/**
 * 初始化表格的列
 */
SdsPrimaryHeader.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '病种', field: 'entityDesc', align: 'center', valign: 'middle', sortable: true, visible: false},
        {title: '表头名称', field: 'title', align: 'center', valign: 'middle', sortable: true},
        {title: '占列数', field: 'colspan', align: 'center', valign: 'middle', sortable: true},
        {title: '排序', field: 'sort', align: 'center', valign: 'middle', sortable: true},
        {title: '是否有效', field: 'btIsActive', align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }},
        {title: '操作', field: 'id', align: 'center',visible: false, width: '100px', valign: 'middle',formatter:function(value,row,index){
                var _html = '<a href="javascript:SdsStandDic.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
                _html += '<a href="javascript:SdsStandDic.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
                return _html;
            }}];
};

/**
 * 点击一级表头，加载对应二级表头
 */
SdsPrimaryHeader.onClickDic = function (obj,$ele,field){
    SdsPrimaryHeader.seItem = obj;
    SdsPrimaryHeader.primaryHeaderId = obj.id;
    SdsPrimaryHeader.primaryHeaderDesc = obj.title;
    SdsSecondaryHeader.search();
}

/**
 * 检查是否选中
 */
SdsPrimaryHeader.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsPrimaryHeader.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增一级表头
 */
SdsPrimaryHeader.add = function () {
    if (SdsQcentity.entityId){
        var index = Sys.open('新增一级表头',Sys.backendPath + '/sds/sdsindconfig/primary/to_add',['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#entityId").attr("value", SdsQcentity.entityId);
            $(iframeWin.document).find("#entityDesc").attr("value", SdsQcentity.entityDesc);
        });
    }else {
        Sys.info("请先选中某一数据源！");
        return false;
    }
    this.layerIndex = index;
};

/**
 * 打开查看一级表头详情
 */
SdsPrimaryHeader.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('一级表头详情',Sys.backendPath + '/sds/sdsindconfig/primary/to_update/' + id + "/",['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#entityDesc").attr("value",SdsQcentity.entityDesc);
        });
        this.layerIndex = index;
    }
};

/**
 * 删除一级表头
 */
SdsPrimaryHeader.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsindconfig/primary/delete", function (data) {
                Sys.success("删除成功!");
                SdsPrimaryHeader.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdsPrimaryHeaderId",id);
            ajax.start();
        };
        Sys.confirm("是否删除一级表头?",operation);
    }
};


/**
 * 查询项目标准化字典列表
 */
SdsPrimaryHeader.search = function () {
    var queryData = {};
    queryData['qcEntity'] =  SdsQcentity.entityId;
    SdsPrimaryHeader.table.refresh({query: queryData});
};

/**
 * 二级表头初始化
 */
var SdsSecondaryHeader = {
    id: "SdsSecondaryHeaderTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsSecondaryHeader.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '一级表头', field: 'primaryHeaderId', align: 'center', valign: 'middle', sortable: true, visible: false},
        {title: '二级表头名称', field: 'title', align: 'center', valign: 'middle', sortable: true},
        {title: '二级表头字段', field: 'field', align: 'center', valign: 'middle', sortable: true},
        {title: '排序', field: 'sort', align: 'center', valign: 'middle', sortable: true},
        {title: '是否有效', field: 'btIsActive', align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }},
        {title: '操作', field: 'id', align: 'center',visible: false, valign: 'middle',formatter:function(value,row,index){
                var _html = '<a href="javascript:SdsStandDicsub.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
                _html += '<a href="javascript:SdsStandDicsub.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
                return _html;
            }}];
};

/**
 * 检查是否选中
 */
SdsSecondaryHeader.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsSecondaryHeader.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增二级表头
 */
SdsSecondaryHeader.add = function () {
    if (SdsPrimaryHeader.primaryHeaderId) {
        var index = Sys.open('新增二级表头',Sys.backendPath + '/sds/sdsindconfig/secondary/to_add',['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#entityId").attr("value", SdsQcentity.entityId);
            $(iframeWin.document).find("#primaryHeaderId").attr("value", SdsPrimaryHeader.primaryHeaderId);
            $(iframeWin.document).find("#primaryHeaderDesc").attr("value", SdsPrimaryHeader.primaryHeaderDesc);
        });
    }else {
        Sys.info("请先选中某一级表头！");
        return false;
    }
    this.layerIndex = index;
};

/**
 * 打开查看二级表头详情
 */
SdsSecondaryHeader.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('二级表头详情',Sys.backendPath + '/sds/sdsindconfig/secondary/to_update/' + id + "/",['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#primaryHeaderDesc").attr("value", SdsPrimaryHeader.primaryHeaderDesc);
        });
        this.layerIndex = index;
    }
};

/**
 * 删除二级表头
 */
SdsSecondaryHeader.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsindconfig/secondary/delete", function (data) {
                Sys.success("删除成功!");
                SdsSecondaryHeader.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdsSecondaryHeaderId",id);
            ajax.start();
        };
        Sys.confirm("是否删除二级表头?",operation);
    }
};

/**
 * 查询二级表头列表
 */
SdsSecondaryHeader.search = function () {
    var queryData = {};
    queryData['primaryHeaderId'] =  SdsPrimaryHeader.primaryHeaderId;
    SdsSecondaryHeader.table.refresh({query: queryData});
};

$(function () {
    //查询病种树
    var ztree = new $ZTree("entityTree", Sys.backendPath + "/sds/sdsqcentity/tree");
    ztree.bindOnClick(SdsQcentity.onClickSource);
    ztree.init();
    SdsQcentity.ztree = ztree;
    fuzzySearch('entityTree','#seachKey',null,true);
    //查询并初始化标准化字典表格
    var primaryHeaderColunms = SdsPrimaryHeader.initColumn();
    var primaryHeadertable = new BSTable(SdsPrimaryHeader.id, Sys.backendPath + "/sds/sdsindconfig/primary/list", primaryHeaderColunms);
    primaryHeadertable.setPaginationType("server");
    primaryHeadertable.setShowExport(false);
    primaryHeadertable.setShowRefresh(false);
    primaryHeadertable.setShowToggle(false);
    primaryHeadertable.setShowColumns(false);
    primaryHeadertable.setOnClickRow(SdsPrimaryHeader.onClickDic)
    primaryHeadertable.setQueryParams({"qcEntity":0});
    primaryHeadertable.setSubHeight(-27);
    SdsPrimaryHeader.table = primaryHeadertable.init();
    //查询并初始化值域表格
    var secondaryHeaderColunms = SdsSecondaryHeader.initColumn();
    var secondaryHeadertable = new BSTable(SdsSecondaryHeader.id, Sys.backendPath + "/sds/sdsindconfig/secondary/list", secondaryHeaderColunms);
    secondaryHeadertable.setPaginationType("server");
    secondaryHeadertable.setShowExport(false);
    secondaryHeadertable.setShowRefresh(false);
    secondaryHeadertable.setShowToggle(false);
    secondaryHeadertable.setShowColumns(false);
    secondaryHeadertable.setQueryParams({"primaryHeaderId":0});
    secondaryHeadertable.setSubHeight(-27);
    SdsSecondaryHeader.table = secondaryHeadertable.init();
});
